<template>
  <el-drawer v-model="dialogVisible" :title="drawerType ? '新增付款记录' : '编辑付款记录'">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
      class="demo-ruleForm2"
    >
      <el-form-item label="付款人信息：" prop="chargePhone">
        <el-select
          v-model="ruleForm.chargePhone"
          placeholder="请选择付款人信息"
          style="width: 100%"
          filterable
        >
          <el-option
            v-for="ite in chargePhoneList"
            :key="ite.label"
            :label="ite.label"
            :value="ite.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类别：" prop="type">
        <el-input v-model="ruleForm.type" placeholder="请输入类别" />
      </el-form-item>
      <el-form-item label="付款金额：" prop="money">
        <el-input type="number" v-model="ruleForm.money" placeholder="请输入付款金额" />
      </el-form-item>
      <el-form-item label="付款时间：" prop="moneyTime">
        <el-date-picker
          v-model="ruleForm.moneyTime"
          type="date"
          placeholder="付款时间"
          format="YYYY/MM/DD"
          value-format="YYYY-MM-DD"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input v-model="ruleForm.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
      </span>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import type { FormInstance } from 'element-plus'
  import {
    postTransactionRecordCreate,
    putTransactionRecordUpdate,
  } from '@/api/controller/transaction-record-controller/index'
  import { postSiteGetChargeInfo } from '@/api/controller/site-controller/index'
  import { ElMessage } from 'element-plus'
  const ruleFormRef = ref<FormInstance>()
  const drawerType = ref(true)
  const chargePhoneList = ref([])
  const ruleForm = ref({
    siteId: history.state.id,
    chargePhone: null,
    type: null,
    money: null,
    moneyTime: null,
    remark: null,
  })

  const rules = reactive({
    chargePhone: [{ required: true, message: '请选择付款人信息', trigger: 'blur' }],
    moneyTime: [{ required: true, message: '请付款时间', trigger: 'blur' }],
    type: [
      { required: true, message: '请输入类别', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    money: [
      { required: true, message: '请输入付款金额', trigger: 'blur' },
      {
        pattern: /^\d+(\.\d{1,2})?$/,
        message: '请输入大于或等于0的数字(最多2位小数)',
      },
    ],
    remark: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })
  const dialogVisible = ref(false)
  //新增或修改
  const submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        if (drawerType.value) {
          const result = await postTransactionRecordCreate(ruleForm.value, { loading: true })
          if (result.code === 200) {
            dialogVisible.value = false
            await emit('getData')
            ElMessage.success('添加成功')
          }
        } else {
          const result = await putTransactionRecordUpdate(ruleForm.value, { loading: true })
          if (result.code === 200) {
            dialogVisible.value = false
            await emit('getData')
            ElMessage.success('修改成功')
          }
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }
  const show = (data) => {
    postSiteGetChargeInfo({}).then((res) => {
      chargePhoneList.value = res.data.map((item) => {
        if (!item) {
          return { label: '空', value: '空' }
        }
        return { label: item, value: item }
      })
    })
    if (data) {
      ruleForm.value = JSON.parse(JSON.stringify(data))
      drawerType.value = false
    } else {
      drawerType.value = true
      for (let key in ruleForm.value) {
        ruleForm.value[key] = null
      }
      ruleForm.value.siteId = history.state.id
    }
    dialogVisible.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>
